import {useStore} from "@/store"

const store = useStore()

export const api = (config = {
    path: "",
    params: "",
    method: "get",
    timeout: 5000,
    isload: true,
    title: "加载中...",
    headers: {}
}) => {
    if (config.isload) {
        uni.showLoading({
            title: config.title,
            mask: true
        })
    }
    return new Promise((resolve, reject) => {
        const token = store.getToken
        uni.request({
            url: config.path,
            // url: config.path.includes('http') ? config.path : `http://localhost:3000/${config.path}`,
            data: config.data,
            method: config.method,
            timeout: config.timeout,
            header: {authorization: token, ...config.headers},
            success(res) {
                resolve(res)
            },
            fail(err) {
                reject(err)
            },
            complete() {
                uni.hideLoading()
            }
        })
    })
}